package uni.UNIBEE7B8D.view.Study;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import uni.UNIBEE7B8D.Adapter.ClassClickOnListener;
import uni.UNIBEE7B8D.Adapter.KechengClickOnListener;
import uni.UNIBEE7B8D.Adapter.RecycClickOnListener;
import uni.UNIBEE7B8D.Adapter.study.BanjiRecycAdapter;
import uni.UNIBEE7B8D.Adapter.study.FruitHorRecyclerVIewAdapter;
import uni.UNIBEE7B8D.Adapter.study.TuijianRecycAdapter;
import uni.UNIBEE7B8D.Adapter.study.ZhiboRecycAdapter;
import uni.UNIBEE7B8D.CommonTool.HttpUrl;
import uni.UNIBEE7B8D.Enty.HomeClass;
import uni.UNIBEE7B8D.Enty.HomeFriend;
import uni.UNIBEE7B8D.Enty.HomeKecheng;
import uni.UNIBEE7B8D.Enty.HomeMenu;
import uni.UNIBEE7B8D.Enty.HomePage;
import uni.UNIBEE7B8D.Enty.HomeZhibo;
import uni.UNIBEE7B8D.Presenter.StudyPersenter;
import uni.UNIBEE7B8D.R;
import uni.UNIBEE7B8D.view.Study.Zhibo.ZhiboList.ZhiboListActivity;
import uni.UNIBEE7B8D.view.Study.ZhiboContent.ZhiboContentActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class StudyFragment extends Fragment implements StudyContract.StudyView , View.OnClickListener {


    @BindView(R.id.pst)
    TabLayout pst;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.zaikanList)
    RecyclerView zaikanList;

    @BindView(R.id.SwipeRefreshLayout)
    androidx.swiperefreshlayout.widget.SwipeRefreshLayout SwipeRefreshLayout;

    @BindView(R.id.zhiboList)
    RecyclerView zhiboList;

    @BindView(R.id.search)
    LinearLayout search;

    @BindView(R.id.tuijianList)
    RecyclerView tuijianList;

    @BindView(R.id.banjiList)
    RecyclerView banjiList;
    @BindView(R.id.moreZhibo)
    TextView moreZhibo;

    private StudyPersenter studyPersenter = new StudyPersenter(this);

    public StudyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_study2, container, false);
        ButterKnife.bind(this, view);//注意
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        moreZhibo.setOnClickListener(this);
        studyPersenter.GetMenu(HttpUrl.aspect, 1, 10, HttpUrl.type);
        //获取轮播图
        studyPersenter.GetData();
        //获取再看
        studyPersenter.GetZaikan("1");
        //获取直播
        studyPersenter.GetZhibo();
        //获取课程
        studyPersenter.GetKecheng(1, 4);
        //获取班级
        studyPersenter.GetClass(1, 3);
        // pst = getView().findViewById(R.id.pst);

        SwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                SwipeRefreshLayout.setRefreshing(false);
                //获取轮播图
                studyPersenter.GetData();
                //获取再看
                studyPersenter.GetZaikan("1");
                //获取直播
                studyPersenter.GetZhibo();
                //获取课程
                studyPersenter.GetKecheng(1, 4);
                //获取班级
                studyPersenter.GetClass(1, 3);

            }
        });

    }

    @Override
    public void Toast(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void GetData(HomePage homePage) {
        List<HomePage.ResultBean> list = new ArrayList<>();
        list = homePage.getResult();

//        List<Integer> LBlist;
//        LBlist = new ArrayList();//数据初始化
//        LBlist.add(R.drawable.bg_zhibo);
//        LBlist.add(R.drawable.bg_zhibo);
        banner.setAdapter(new BannerImageAdapter<HomePage.ResultBean>(list) {
            @Override
            public void onBindView(BannerImageHolder holder, HomePage.ResultBean data, int position, int size) {
                RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(20));//图片圆角为20
                Glide.with(getActivity()).load(data.getPicturesUrl()).apply(options).error(R.mipmap.ic_launcher).into(holder.imageView);
            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                Toast.makeText(getContext(), "点击了" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
            }


        });
    }

    @Override
    public void GetZaikan(HomeFriend homeFriend) {
        List<HomeFriend.ResultBean> list = new ArrayList<>();
        list = homeFriend.getResult();
        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        zaikanList.setLayoutManager(linearLayoutManager);

        FruitHorRecyclerVIewAdapter mainAdapter = new FruitHorRecyclerVIewAdapter(list, getActivity());
        zaikanList.setAdapter(mainAdapter);

        mainAdapter.setRecycClickOnListener(new RecycClickOnListener() {
            @Override
            public void onItemClick(HomeFriend.ResultBean commonClass) {
                Log.d("==", commonClass.getName());
            }

            @Override
            public void onItemZhibo(HomeZhibo.ResultBean.RecordsBean recordsBean) {

            }
        });
    }

    @Override
    public void GetZhibo(HomeZhibo homeZhibo) {
        //--------直播-----------------
        List<HomeZhibo.ResultBean.RecordsBean> list = new ArrayList<>();
        list = homeZhibo.getResult().getRecords();

        // 设置布局管理器
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        zhiboList.setLayoutManager(linearLayoutManager1);

        ZhiboRecycAdapter zhiboRecycAdapter = new ZhiboRecycAdapter(list, getActivity());
        zhiboList.setAdapter(zhiboRecycAdapter);
        zhiboRecycAdapter.setRecycClickOnListener(new RecycClickOnListener() {
            @Override
            public void onItemClick(HomeFriend.ResultBean commonClass) {

            }

            @Override
            public void onItemZhibo(HomeZhibo.ResultBean.RecordsBean commonClass) {
                Log.d("==", commonClass.getId() + "");
                Intent intent = new Intent(getActivity(), ZhiboContentActivity.class);
                intent.putExtra("id", commonClass.getId());
                intent.putExtra("type","zhibo");
                startActivity(intent);
            }
        });
    }

    @Override
    public void GetKecheng(HomeKecheng homeKecheng) {

        List<HomeKecheng.ResultBean> list = new ArrayList<>();
        list = homeKecheng.getResult();
        // -------------推荐课程---------------------

        // 设置布局管理器
        LinearLayoutManager linearLayoutManager2 = new GridLayoutManager(getActivity(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        tuijianList.setLayoutManager(linearLayoutManager2);

        TuijianRecycAdapter tuijianRecycAdapter = new TuijianRecycAdapter(list, getActivity());
        tuijianList.setAdapter(tuijianRecycAdapter);
        tuijianRecycAdapter.setRecycClickOnListener(new KechengClickOnListener() {
            @Override
            public void onItemClick(HomeKecheng.ResultBean commonClass) {
                Intent intent = new Intent(getActivity(), ZhiboContentActivity.class);
                intent.putExtra("id", commonClass.getId());
                intent.putExtra("type","kecheng");
                startActivity(intent);
            }
        });
    }

    @Override
    public void GetClass(HomeClass homeClass) {
        List<HomeClass.ResultBean> list = new ArrayList<>();
        list = homeClass.getResult();
        // 设置布局管理器
        LinearLayoutManager linearLayoutManagerbanji = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        banjiList.setLayoutManager(linearLayoutManagerbanji);

        BanjiRecycAdapter banjiRecycAdapter = new BanjiRecycAdapter(list, getActivity());
        banjiList.setAdapter(banjiRecycAdapter);
        banjiRecycAdapter.setRecycClickOnListener(new ClassClickOnListener() {
            @Override
            public void onItemClick(HomeClass.ResultBean commonClass) {
                Intent intent = new Intent(getActivity(), ZhiboContentActivity.class);
                intent.putExtra("id", commonClass.getId());
                intent.putExtra("type","class");
                startActivity(intent);
            }
        });
    }

    @Override
    public void GetMeny(HomeMenu homeMenu) {
        List<HomeMenu.ResultBean.RecordsBean> list = new ArrayList<>();
        list = homeMenu.getResult().getRecords();
        pst.addTab(pst.newTab().setText("首页"));

        for (HomeMenu.ResultBean.RecordsBean item : list) {
            pst.addTab(pst.newTab().setText(item.getName()));
        }
        pst.setTabRippleColorResource(R.color.bai);

        pst.getTabAt(0).setCustomView(R.layout.tab_layout_text);
        TextView textView = pst.getTabAt(0).getCustomView().findViewById(android.R.id.text1);
        textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize);

        pst.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutUnsectedTextSize);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.moreZhibo:
                Intent intent = new Intent(getActivity(), ZhiboListActivity.class);
                startActivity(intent);
                break;
        }
    }
}
